using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace UKVSTS
{
    [Serializable]
    public class FieldDefaultValue
    {
        private String m_DefaultValue;

        private String m_FieldName;

        public String DefaultValue
        {
            get { return m_DefaultValue; }
            set { m_DefaultValue = value; }
        }

        public String FieldName
        {
            get { return m_FieldName; }
        }

        public FieldDefaultValue(String fieldName)
        {
            ArgTest.IsNotNullOrEmpty(fieldName, "fieldName");
            m_FieldName = fieldName;
        }

        public FieldDefaultValue(String fieldName, String defaultValue)
            : this (fieldName)
        {
            ArgTest.IsNotNullOrEmpty(defaultValue, "defaultValue");
            m_DefaultValue = defaultValue;
        }

        public override bool Equals(object obj)
        {
            bool equals = false;

            if (Object.ReferenceEquals(obj, this))
            {
                equals = true;
            }
            else
            {
                FieldDefaultValue other = obj as FieldDefaultValue;
                if (other != null)
                {
                    equals = other.FieldName.Equals(FieldName);
                }
            }
            return equals;
        }

        public override int GetHashCode()
        {
            return FieldName.GetHashCode();
        }

        public bool ApplyToWorkItem(WorkItem wi, out String reason)
        {
            ArgTest.IsNotNull(wi, "wi");

            bool success = false;
            if (wi.Fields.Contains (FieldName) == false)
            {
                reason = String.Format("The field \"{0}\" is not defined in the \"{1}\" work item type",
                                        FieldName, wi.Type.Name);
            }
            else
            {
                Field f = wi.Fields[FieldName];
                
                if (f.IsEditable == false)
                {
                    reason = String.Format("The field \"{0}\" is not editable", FieldName);
                }
                else
                {

                    if (f.IsLimitedToAllowedValues && f.AllowedValues.Contains(DefaultValue) == false)
                    {
                        reason = String.Format("The value \"{0}\" is not valid for the \"{1}\" field at this time", 
                                               DefaultValue, FieldName);
                    } else {
                        try
                        {
                            f.Value = DefaultValue;
                            success = true;
                            reason = "Success";
                        }
                        catch (Exception ex)
                        {
                            reason = ex.Message;
                        }
                    }
                }
            }
            return success;
        }
    }
}